package com.fili.gui;

import com.fili.gui.wizards.WizardPanel;
import com.fili.gui.wizards.WizardUser;
import com.fili.gui.components.StatusBar;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.html.HTMLEditorKit;
import java.util.Stack;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 17, 2006
 * Time: 4:26:20 PM
 * Revision: 0
 */
public class WizardFrame extends JFrame implements ActionListener
{
    public static final int BUTTON_BACK = 1;
    public static final int BUTTON_NEXT = 2;
    public static final int BUTTON_FINISH = 3;

    private JTextPane helpArea;
    private JPanel mainPanel;
    private JButton btnBack;
    private JButton btnNext;
    private JButton btnFinish;
    private StatusBar statusBar;

    private Stack wizardStack;

    private WizardPanel current;


    public WizardFrame()
    {
        wizardStack = new Stack();
        current = null;
        mainPanel = new JPanel();
        statusBar = new StatusBar();
        helpArea = new JTextPane();

        btnBack = new JButton("Back");
        btnBack.addActionListener(this);
        btnNext = new JButton("Next");
        btnNext.addActionListener(this);
        btnFinish = new JButton("Finish");
        btnFinish.addActionListener(this);


        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(statusBar, BorderLayout.SOUTH);

        JPanel bigPanel = new JPanel(new BorderLayout());
        JScrollPane sp = new JScrollPane(helpArea);
        sp.setPreferredSize(new Dimension(200, 200));
        getContentPane().add(sp, BorderLayout.WEST);

        JPanel smallPanel = new JPanel(new BorderLayout());
        JPanel bottomPnl = new JPanel(new BorderLayout());
        JPanel tmpPnl = new JPanel(new GridLayout(1, 3, 5, 5));
        tmpPnl.add(btnBack);
        tmpPnl.add(btnNext);
        tmpPnl.add(btnFinish);
        bottomPnl.add(tmpPnl, BorderLayout.EAST);
        bottomPnl.setBorder(new EmptyBorder(5, 5, 5, 5));

        smallPanel.add(mainPanel, BorderLayout.CENTER);
        smallPanel.add(bottomPnl, BorderLayout.SOUTH);

        bigPanel.add(smallPanel, BorderLayout.CENTER);


        getContentPane().add(bigPanel, BorderLayout.CENTER);

        pushWizard(new WizardUser(this));

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 400);
        setVisible(true);
    }

    public void setHelp(String text)
    {
        helpArea.setText(text);
    }

    public void pushWizard(WizardPanel panel)
    {
        wizardStack.push(panel);
        setWizard(panel);
    }

    public void popWizard()
    {
        wizardStack.pop();
        if (!wizardStack.isEmpty())
            setWizard((WizardPanel) wizardStack.peek());
        else
            setWizard(null);
    }

    private void setWizard(WizardPanel panel)
    {
        mainPanel.removeAll();
        mainPanel.setLayout(new BorderLayout());
        if (panel != null)
            mainPanel.add(new JScrollPane(panel), BorderLayout.CENTER);
        current = panel;
        mainPanel.revalidate();
        mainPanel.repaint();
    }

    public void enableButton(int button, boolean enabled)
    {
        switch (button)
        {
            case BUTTON_BACK:
                btnBack.setEnabled(enabled);
                break;
            case BUTTON_NEXT:
                btnNext.setEnabled(enabled);
                break;
            case BUTTON_FINISH:
                btnFinish.setEnabled(enabled);
                break;
        }
    }

    public void enableButtons(boolean back, boolean next, boolean finish)
    {
        btnBack.setEnabled(back);
        btnNext.setEnabled(next);
        btnFinish.setEnabled(finish);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource().equals(btnBack))
        {
            if (current != null)
            {
                current.onBack();
                popWizard();
                if (current != null)
                    current.onReturn();
            }
        }
        if (e.getSource().equals(btnNext))
        {
            if (current != null)
                current.onNext();
        }
        if (e.getSource().equals(btnFinish))
        {
            if (current != null)
                current.onFinish();
        }
    }
}
